<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/1
 * Time: 17:00
 */
namespace app\autoreply\controller;

use app\common\config\autoreply_config;
use app\common\helper\appmsg_helper;
use app\common\helper\autoreply_helper;
use app\common\model\Appmsg;
use app\common\model\AutoreplyContent;
use app\common\model\AutoreplyKey;
use think\facade\Config;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Admin extends AdminBase
{
    private $autoreply_type;
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->autoreply_type = autoreply_config::$autoreply_type;
    }

    public function __call($action = '', $params = array())
    {
        $content_type = Request::param('content_type', 1);

        if (!array_key_exists($action.'', $this->autoreply_type)) {
            $action = 1;
        }

        if ($action == 1 || $action == 2) {
            $reply_list = autoreply_helper::get_autoreply_list($action);
            if (!$reply_list) {
                return redirect(url('/autoreply/add/'.$action));
            }
        }

        View::assign([
            'autoreply_type' => $action,
            'content_type'   => $content_type

        ]);

        if ($action == 4 ) {
            return $this->get_auto_key_list('activity', $action);
        }

        // 关键词的回复
        if ($action == 3) {
            return $this->get_auto_key_list('autokey_list', $action);

            // 关注时的回复or不能识别时的回复
        } else {
            $content_info = autoreply_helper::get_autoreply_content_by_type($action, 1, $this->open_card_id, $this->company_id);

            if (!$content_info) {
                return redirect(url('/autoreply/add/'.$action.'?content_type='.$content_info['content_type']));
            }

            return View::fetch('index', [
                'content_info' => $content_info,
            ]);
        }
    }

    private function get_auto_key_list($tpl, $type)
    {
        $page_no         = Request::param('page_no', 1);
        $is_advenced     = Request::param('is_advenced', 0);
        $search_filter   = Request::param('search', array());

        $auto_key_list = array();
        $filter = array(
            'company_id'     => $this->company_id,
            'open_card_id'   => $this->open_card_id,
            'type'           => $type
        );
//        if (isset($search_filter['title']) && (trim($search_filter['title']) || trim($search_filter['title']) == "0")) {
//            $filter['title like'] = '%'.strip_tags($search_filter['title']).'%';
//        }
//        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
//            $filter['add_time >='] = strip_tags($search_filter['start_date']).' 00:00:00';
//        }
//        if (isset($search_filter['stop_date']) && $search_filter['stop_date']) {
//            $filter['add_time <='] = strip_tags($search_filter['stop_date']).' 23:59:59';
//        }

        $auto_key_list = getList('autoreply', $filter, ['id', 'desc']);

        return View::fetch($tpl, [
            'search_filter' => $search_filter,
            'is_advenced'   => $is_advenced,
            'list'          => $auto_key_list
        ]);
    }


    /**
     * 更改匹配规则
     * @return array
     */
    public function change_match_type()
    {
        $id          = Request::post('id', 0);
        $key_id      = Request::post('key_id', 0);
        $match_type  = Request::post('match_type', 1);

        if (!$id) {
            return ['info' => '该关键词不存在'];
        }
        $info = _uri('autoreply', ['id' => $id]);
        if (!$info) {
            return ['info' => '该关键词不存在'];
        }
        if ($match_type == 1) {
            $match_type = 2;
        } else {
            $match_type = 1;
        }

        AutoreplyKey::update(['match_type'=>$match_type], ['id'=>$key_id]);

        return array('info'=>'ok', 'match_type'=>$match_type);
    }

    public function cancel_reply()
    {
        $type = Request::param('type', 0);

        if (!$type) {
            return ['info' => '请传递要取消的回复类型'];
        }
        //$content_info = autoreply_helper::get_autoreply_content_by_type($action);

        $autoreply_list = autoreply_helper::get_autoreply_content_by_type($type, 1, $this->open_card_id, $this->company_id);;
        if (!$autoreply_list || !isset($autoreply_list[0]['id']) || !$autoreply_list[0]['id']) {
            return ['info' =>'无回复，不用取消'];
        }

        $autoreply_id = (int)$autoreply_list[0]['id'];
        //_model('autoreply_content')->delete(array('autoreply_id'=>$autoreply_id));
        AutoreplyContent::where(['autoreply_id'=>$autoreply_id])->delete();

        return ['info' =>'ok'];
    }
}